package com.blockchain.front.servlets.financial;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ObjectUtils.Null;

import com.blockchain.common.enums.GdState;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.front.service.common.entity.GdEntity;
import com.blockchain.front.service.financial.GdManage;
import com.blockchain.front.servlets.security.AbstractSceurityServlet;
import com.blockchain.front.servlets.trading.CommentMarket;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

public class UpdatePd extends AbstractSceurityServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		int id = IntegerParser.parse(request.getParameter("id"));
		GdManage manage = serviceSession.getService(GdManage.class);
		GdEntity gdEntity = manage.get(id);
		if (id <= 0 || null == gdEntity ) {
			throw new ParameterException("该订单不存在");
		}
		String type = request.getParameter("type");
		request.setAttribute("gdEntity", gdEntity);
		request.setAttribute("type",type ==null?-1:type);
		forwardView(request, response,UpdatePd.class);
	}

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		request.setAttribute("type", -1);
		int id = IntegerParser.parse(request.getParameter("id"));
		GdState gdState = EnumParser.parse(GdState.class, request.getParameter("gdState"));
		GdManage manage = serviceSession.getService(GdManage.class);
		if (null != request.getParameter("pdsl") && (!"".equals(request.getParameter("pdsl")))) {
			BigDecimal pdsl = new BigDecimal(request.getParameter("pdsl"));
			String pdrxm = request.getParameter("pdrxm");
			String pdrdh = request.getParameter("pdrdh");
			manage.pxGd(id,gdState,pdsl,pdrxm,pdrdh);
		}else {
			throw new ParameterException("拍单数量不能为空");
		}
		sendRedirect(request, response, getController().getURI(request, GdList.class));
	}
	
	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		getResourceProvider().log(throwable.getMessage());
		int id = IntegerParser.parse(request.getParameter("id"));
		String url=getController().getViewURI(request,UpdatePd.class)+"?id="+id+"&type=-1";
		if (throwable instanceof LogicalException || throwable instanceof ParameterException) {
			prompt(request, response, PromptLevel.ERROR, throwable.getMessage());
			sendRedirect(request, response,url);
		}  else {
			super.onThrowable(request, response, throwable);
		}
	}

}